package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
)

type JiaochengsRBody struct {
	Data       []models.Jiaocheng `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}
type JiaochengsBody struct {
	PageNo   int    `json:"pageNo"`
	Pagesize int    `json:"pagesize"`
	ID       int64  `json:"id" gorm:"id"`
	Video    string `json:"video" gorm:"video"`
	Title    string `json:"title" gorm:"title"`
	Image    string `json:"image" gorm:"image"`
	Name     string `json:"name" gorm:"name"`
	Shijian  string `json:"shijian" gorm:"shijian"`
	Area     string `json:"area" gorm:"area"`
	Content  string `json:"content" gorm:"content"`
}

func Jiaochengdel(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Jiaocheng{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Jiaochengupdate(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	video := request.Video
	title := request.Title
	jc := models.Jiaocheng{
		Video: video,
		Title: title,
	}
	data_source.Db.Where("id = ?", id).Updates(&jc)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Jiaochengadd(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	video := request.Video
	title := request.Title
	jc := models.Jiaocheng{
		Video: video,
		Title: title,
	}
	data_source.Db.Create(&jc)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Jiaochengs(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Jiaocheng
	res := data_source.Db
	res.Model(&models.Jiaocheng{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := JiaochengsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type KechengsRBody struct {
	Data       []models.Kecheng `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

func Kechengdel(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Kecheng{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Kechengupdate(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	image := request.Image
	name := request.Name
	area := request.Area
	shijian := request.Shijian
	jc := models.Kecheng{
		Image:   image,
		Name:    name,
		Area:    area,
		Shijian: shijian,
	}
	data_source.Db.Where("id = ?", id).Updates(&jc)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Kechengadd(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	image := request.Image
	name := request.Name
	area := request.Area
	shijian := request.Shijian
	jc := models.Kecheng{
		Image:   image,
		Name:    name,
		Area:    area,
		Shijian: shijian,
	}
	data_source.Db.Create(&jc)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Kechengs(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Kecheng
	res := data_source.Db
	res.Model(&models.Kecheng{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := KechengsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type KechengbmsRBody struct {
	Data       []models.Kechengbmsql `json:"data"`
	PageNo     int                   `json:"pageNo"`
	PageSize   int                   `json:"pageSize"`
	TotalCount int64                 `json:"totalCount"`
	TotalPage  int64                 `json:"totalPage"`
}

func Kechengbms(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Kechengbmsql
	res := data_source.Db.Model(&models.Kechengbm{}).Select("kechengbm.*, hongniang.name, hongniang.phone").Joins("left join hongniang on hongniang.id = kechengbm.hongniangid").Where("kechengbm.kechengid = ?", id)

	res.Model(&models.Kechengbm{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := KechengbmsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
